package redis

import "C"
import (
	"GoWeb_Advanced/00_bluebell/settings"
	"fmt"

	"github.com/go-redis/redis"
)

// 声明一个全局的 rdb 变量
var (
	client *redis.Client
	Nil    = redis.Nil
)

// Init 初始化连接
func Init(config *settings.RedisConfig) (err error) {
	client = redis.NewClient(&redis.Options{
		Addr: fmt.Sprintf("%s:%d",
			config.Host,
			config.Port,
		),
		Password:     config.Password, //取不到的话，默认值为空
		DB:           config.DB,
		PoolSize:     config.PoolSize,
		MinIdleConns: config.MinIdleConns,
	})

	_, err = client.Ping().Result()
	return
}

func Close() {
	_ = client.Close()
}
